package com.collectplus.express.self;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.collectplus.express.BaseActivity2;
import com.collectplus.express.R;

import java.util.ArrayList;
import java.util.List;

import droid.frame.view.ViewPagerExt;

/**
 * Created by joker on 2017/8/24.
 * 条形码查看示例
 */
public class CheckQRCodeDialogActivity extends BaseActivity2 implements View.OnClickListener {

    private ViewPagerExt mViewPager;
    private LinearLayout mCheckLayout;
    private final int PAGE_SIZE = 2;// 示例数量

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 第二个方法在此参数无效
        overridePendingTransition(R.anim.in_bottom2top, 0);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.self_checkqr_code);
        super.findViewById();
        mViewPager = (ViewPagerExt) findViewById(R.id.view_pager);
        mCheckLayout = (LinearLayout) findViewById(R.id.check_dot_layout);
        findViewById(R.id.finish_img).setOnClickListener(this);
        loadImage();
        selectPage(0);
        mViewPager.addOnPageChangeListener(mViewPager.new ViewPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                selectPage(position);
            }
        });
    }

    private void loadImage() {
        List<View> pager = new ArrayList<View>();
        for (int i = 0; i < PAGE_SIZE; i++) {
            int resId = getResources().getIdentifier("send_scan_pic_" + (i + 1), "drawable", getPackageName());
            LinearLayout parms = new LinearLayout(getContext());
            parms.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
            parms.setBackgroundResource(resId);
            pager.add(parms);
        }
        mViewPager.setViewItems(pager,false);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.finish_img:
                finish();
                break;
        }
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, R.anim.out_top2bottom);
    }

    /**
     * 设置选中的页
     *
     * @param position
     */
    private void selectPage(int position) {
        for (int i = 0; i < mCheckLayout.getChildCount(); i++) {
            ImageView child = (ImageView) mCheckLayout.getChildAt(i);
            if (position == i) {
                child.setImageResource(R.drawable.dot_light);
            } else {
                child.setImageResource(R.drawable.dot_dark);
            }
        }
    }
}

